package com.bjc.platform.security;

import java.io.IOException;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.SimpleUrlAuthenticationFailureHandler;

import com.bjc.platform.model.AdmLog;
import com.bjc.platform.repository.AdmLogRepo;
import com.bjc.platform.util.IpUtil;

/**
 * handler for authentication failure
 * 
 * @author BJC
 */
public class AuthenticationFailureHandler extends
		SimpleUrlAuthenticationFailureHandler {

	@Autowired
	private AdmLogRepo admLogRepo;

	/**
	 * 自定义认证失败处理
	 */
	@Override
	public void onAuthenticationFailure(HttpServletRequest request,
			HttpServletResponse response, AuthenticationException exception)
			throws ServletException, IOException {

		AdmLog admLog = new AdmLog();
		admLog.setSource(IpUtil.getIpAddr(request));
		admLog.setType("WARN");
		admLog.setUrl(request.getRequestURI());
		admLog.setFuncName("login");
		admLog.setDescription(exception.getMessage());
		admLog.setLogTime(new Date());
		// admLog.setUserId(authentication.);
		admLogRepo.save(admLog);

		super.onAuthenticationFailure(request, response, exception);
	}
}
